//
//  MAAdvNodeView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/7.
//

#import "MAAdvNodeView.h"

@interface MAAdvNodeView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, assign) NSUInteger index;

@end


@implementation MAAdvNodeView

- (void)refreshContentWithTag:(NSUInteger)tag {
    if (tag == 0) {
        UIImageView *imageView = [UIImageView initWithName:@"Adv_Begin"];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.size.mas_equalTo(self);
        }];
    } else if (tag == NODE_NUM - 1) {
        [self addSubview:self.imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        UIImageView *imageView = [UIImageView initWithName:@"Adv_End"];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.centerY.mas_equalTo(-8);
            make.size.mas_equalTo(self);
        }];
    } else {
        [self addSubview:self.imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        UILabel *label = [UILabel new];
        label.text = [NSString stringWithFormat:@"%ld", tag];
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = UIColor.whiteColor;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        _index = tag;
        
        [self addSubview:self.button];
        [_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    
    
}

- (void)refreshBackColorWithValue:(NSUInteger)value {
    if (value >= 0 && value < 4 && _imageView) {
        _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Adv_%ld", value]];
    }
}

- (void)buttonEvent {
    MABlock(self.clickBlcok, _index);
}

- (UIImageView *)imageView {
    if (_imageView) return _imageView;
    _imageView = [UIImageView new];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    return _imageView;
}

- (UIButton *)button {
    if (_button) return _button;
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    return _button;
}

@end
